﻿using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace Module.NumberGame.Views
{
    /// <summary>
    /// Interaction logic for NumberBox
    /// </summary>
    public partial class NumberBox : UserControl
    {
        public int InitRow;
        public int InitCol;
        public int PieceLevel;
        private Storyboard myStoryBoard = new Storyboard() { FillBehavior = FillBehavior.Stop };
        public NumberBox(int initCol=0, int initRow = 0, int pieceLevel=0)
        {
            InitializeComponent();
            InitRow = initRow;
            InitCol = initCol;
            SetLevel(pieceLevel);
            Grid.SetRow(this, InitRow);
            Grid.SetColumn(this, InitCol);
        }

        public void InitPos()
        {
           
        }
        public void SetLevel(int level)
        {
            PieceLevel = level;
            if (level is >= 0 and <= 10) this.Background = this.FindResource($"Number_{level}") as ImageBrush; // 设置图片
            else this.Background = null;

        }
        public void MoveSteps(int colstep,int rowstep)
        {
            double disX;
            double disY;
            disX = colstep * 96;
            disY = rowstep * 96;
            Debug.Print($"{disX},{disY}");
            DoubleAnimation moveX = new DoubleAnimation()
            {
                To = disX,
                Duration = TimeSpan.FromMilliseconds(Math.Abs(disX)*3)

            };
            Storyboard.SetTargetName(moveX, this.Name);
            Storyboard.SetTargetProperty(moveX, new PropertyPath("(Grid.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)"));

            DoubleAnimation moveY = new DoubleAnimation()
            {
                To= disY,
                Duration = TimeSpan.FromMilliseconds(Math.Abs(disY)*3)

            };
            Storyboard.SetTargetName(moveY, this.Name);
            Storyboard.SetTargetProperty(moveY, new PropertyPath("(Grid.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)"));
            
            myStoryBoard.Children.Add(moveX);
            myStoryBoard.Children.Add(moveY);
            myStoryBoard.Completed += MyStoryBoard_Completed;
            myStoryBoard.Begin(this);
        }

        private void MyStoryBoard_Completed(object sender, EventArgs e)
        {
            myStoryBoard.Children.Clear();
            myStoryBoard.Remove(this);
            this.SetLevel(-1);
        }

    }
}
